/**
 * Copyright (c) 2018, wuyy All rights reserved.
 * wuyy 专有/保密源代码,未经许可禁止任何人通过任何渠道使用、修改源代码.
 * QQ:16349023
 * 项目名称 : com.wy2.eclipse.tool.ui
 * @version V1.0
 */
package com.wy2.eclipse.tool.ui.editors;

import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.TreeItem;
import com.wy2.core.tool.StringTools;
import com.wy2.generator.database.entity.DatabaseTable;

/**
 * 数据表单元格修改器
 * @author wuyy<16349023@qq.com>
 * @date 2018年1月21日上午10:55:09
 * 
 */
public class TableCellModifier implements ICellModifier {
    
    public static final String COLUMN_0="sqlName";
    public static final String COLUMN_1="property";
    public static final String COLUMN_2="property-type";
    public static final String COLUMN_3="module";
    
    
    
    
    private final TreeViewer treeViewer;
    
    
    

    public TableCellModifier(TreeViewer treeViewer) {
        super();
        this.treeViewer = treeViewer;
    }

    /** 
     * @param element
     * @param property
     * @return 
     * @see org.eclipse.jface.viewers.ICellModifier#canModify(java.lang.Object, java.lang.String)
     */
    @Override
    public boolean canModify(Object element, String property) {
        if(element instanceof DatabaseTable) {
            if(COLUMN_3.equals(property)) {
                return true;
            }
        }
        return false;
    }

    /** 
     * @param element
     * @param property
     * @return 
     * @see org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.Object, java.lang.String)
     */
    @Override
    public Object getValue(Object element, String property) {
        if(element instanceof DatabaseTable) {
            DatabaseTable table=(DatabaseTable)element;
            if(COLUMN_3.equals(property)) {
                return StringTools.isEmpty(table.getModule()) ? "" : table.getModule().toLowerCase();
            }
        }
        return null;
    }

    /** 
     * @param element
     * @param property
     * @param value 
     * @see org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Object, java.lang.String, java.lang.Object)
     */
    @Override
    public void modify(Object element, String property, Object value) {
        TreeItem item=(TreeItem) element;
        Object obj=item.getData();
        if(obj instanceof DatabaseTable) {
            DatabaseTable table=(DatabaseTable)obj;
            if(COLUMN_3.equals(property)) {
               table.setModule((String)value);
               treeViewer.refresh();
            }
        }

    }

}
